<template>
  <div class="container">
    <!--工具栏-->
    <div
      class="toolbar"
      style="float: left; padding-top: 10px; padding-left: 15px"
    >
      <el-form :inline="true" :model="filters" :size="size">
        <el-form-item label="姓名" prop="name">
          <el-input
            v-model="filters.name"
            placeholder="名称"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item label="学期" prop="termId">
          <el-select v-model="filters.termId" placeholder="请选择" clearable>
            <el-option
              v-for="item in termOptions"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="班级" prop="deptName">
          <popup-tree-input
            :data="deptData"
            :props="deptTreeProps"
            :prop="filters.deptName"
            :nodeKey="'' + filters.deptId"
            :currentChangeHandle="deptTreeCurrentChangeHandle"
          >
          </popup-tree-input>
        </el-form-item>

        <el-form-item>
          <kt-button
            :label="$t('action.search')"
            perms="sys:busSynthesis:search"
            type="primary"
            @click="findPage(null)"
          />
        </el-form-item>
        <el-form-item>
          <kt-button
            :label="$t('action.export')"
            perms="sys:busSynthesis:export"
            type="primary"
            @click="handleExport"
          />
        </el-form-item>
        <el-form-item>
          <kt-button
            :label="$t('action.calculation')"
            perms="sys:busSynthesis:calculation"
            type="primary"
            @click="handleCalculation"
          />
        </el-form-item>
        <el-form-item>
          <kt-button
            :label="$t('action.clear')"
            perms="sys:busSynthesis:clear"
            type="primary"
            @click="handleClear"
          />
        </el-form-item>
      </el-form>
    </div>
    <!--表格内容栏-->
    <kt-table
      :pageType="pageType"
      :data="pageResult"
      :columns="columns"
      @findPage="findPage"
      :showBatchDelete="showOperation"
      :showOperation="showOperation"
    >
    </kt-table>
  </div>
</template>

<script>
import KtTable from "@/views/Core/KtTable";
import KtButton from "@/views/Core/KtButton";
import { format } from "@/utils/datetime";
import PopupTreeInput from "@/components/PopupTreeInput";
export default {
  components: {
    KtTable,
    KtButton,
    PopupTreeInput,
  },
  data() {
    return {
      size: "small",
      pageType: 1,
      showOperation: false,
      filters: {
        name: "",
        deptId: "",
        deptName: "",
        termId: "",
      },
      columns: [
        { prop: "studentNum", label: "学号", minWidth: 100 },
        { prop: "username", label: "姓名", minWidth: 100 },
        { prop: "termname", label: "学期", minWidth: 100 },
        { prop: "deptname", label: "班级", minWidth: 100 },
        // {prop:"moralD", label:"德育宿舍分", minWidth:100},
        // {prop:"moralE", label:"德育其他", minWidth:100},
        // {prop:"moralF", label:"德育测评分", minWidth:100},
        { prop: "moralTotal", label: "德育总分", minWidth: 100 },
        { prop: "moralOrder", label: "德育排名", minWidth: 100 },
        // {prop:"intellectualFlag", label:"智育不及格标识", minWidth:100},
        { prop: "intellectualTotal", label: "智育总分", minWidth: 100 },
        { prop: "intellectualOrder", label: "智育排名", minWidth: 100 },
        // {prop:"expandD", label:"扩展附件分", minWidth:100},
        // {prop:"expandE", label:"扩展基础分", minWidth:100},
        { prop: "expandTotal", label: "扩展总分", minWidth: 100 },
        { prop: "expandOrder", label: "扩展排名", minWidth: 100 },
        { prop: "sportsC", label: "体育活动成绩", minWidth: 100 },
        // {prop:"sportTotal", label:"体育总分", minWidth:100},
        { prop: "sportOrder", label: "体育排名", minWidth: 100 },
        { prop: "synthesisTotal", label: "总分", minWidth: 100 },
        { prop: "synthesisOrder", label: "排名", minWidth: 100 },
      ],
      pageRequest: { pageNum: 1, pageSize: 8 },
      pageResult: {},

      operation: false, // true:新增, false:编辑
      editDialogVisible: false, // 新增编辑界面是否显示
      editLoading: false,
      dataFormRules: {
        label: [{ required: true, message: "请输入名称", trigger: "blur" }],
      },
      // 新增编辑界面数据
      dataForm: {
        id: null,
        studentNum: null,
        termId: null,
        sportsC: null,
        sportTotal: null,
        sportOrder: null,
      },
      termOptions: [],
      userOptions: [],
      //保存上传文件流
      itemFile: {},
      importDialogVisible: false,
      importInline: {
        termId: null,
        fileName: null,
      },
      deptData: [],
      deptTreeProps: {
        label: "name",
        children: "children",
      },
      showAuditB: true,
    };
  },
  methods: {
    // 获取分页数据
    async findPage(data) {
      await this.$api.busTerm.findCurrentTerm().then((res) => {
        if (!this.filters.termId || this.filters.termId == "") {
          this.$set(this.filters, "termId", res.data.id);
        }
      });
      if (data !== null) {
        this.pageRequest = data.pageRequest;
      }

      this.pageRequest.params = [
        { name: "name", value: this.filters.name },
        { name: "deptId", value: this.filters.deptId },
        { name: "termId", value: this.filters.termId },
      ];
      await this.$api.busSynthesis
        .findPage(this.pageRequest)
        .then((res) => {
			if(res.code!=500){
				this.pageResult = res.data;

			}
			console.log(res);
			
        })
        .then(data != null ? data.callback : "");
    },
    findPageTo: function (data) {
      if (data !== null) {
        this.pageRequest = data.pageRequest;
      }
      this.pageRequest.params = [
        { name: "name", value: this.filters.name },
        { name: "deptId", value: this.filters.deptId },
        { name: "termId", value: this.filters.termId },
      ];
      this.$api.busSynthesis
        .findPageTo(this.pageRequest)
        .then((res) => {
          this.pageResult = res.data;
        })
        .then(data != null ? data.callback : "");
    },
    // 批量删除
    handleDelete: function (data) {
      this.$api.busSynthesis
        .batchDelete(data.params)
        .then(data != null ? data.callback : "");
    },
    // 显示新增界面
    handleAdd: function () {
      this.editDialogVisible = true;
      this.operation = true;
      this.dataForm = {
        id: null,
        studentNum: null,
        termId: null,
        sportsC: null,
        sportTotal: null,
        sportOrder: null,
      };
    },
    // 显示编辑界面
    handleEdit: function (params) {
      this.editDialogVisible = true;
      this.operation = false;
      this.dataForm = Object.assign({}, params.row);
    },
    // 编辑
    submitForm: function () {
      this.$refs.dataForm.validate((valid) => {
        if (valid) {
          this.$confirm("确认提交吗？", "提示", {}).then(() => {
            this.editLoading = true;
            let params = Object.assign({}, this.dataForm);
            this.$api.busSynthesis.save(params).then((res) => {
              if (res.code == 200) {
                this.$message({ message: "操作成功", type: "success" });
              } else {
                this.$message({
                  message: "操作失败, " + res.msg,
                  type: "error",
                });
              }
              this.editLoading = false;
              this.$refs["dataForm"].resetFields();
              this.editDialogVisible = false;
              this.findPage(null);
            });
          });
        }
      });
    },
    // 时间格式化
    dateFormat: function (row, column, cellValue, index) {
      return format(row[column.property]);
    },
    // 状态格式化
    statusFormat: function (row, column, cellValue, index) {
      if (row.status == 1) {
        return "待提交";
      } else if (row.status == 2) {
        return "班管退回";
      } else if (row.status == 3) {
        return "班管审核中";
      } else if (row.status == 4) {
        return "院管退回";
      } else if (row.status == 5) {
        return "院管审核中";
      } else if (row.status == 6) {
        return "完成";
      }
    },
    getTermOptions: function () {
      this.$api.busTerm.findOptions(0).then((res) => {
        this.termOptions = res.data;
      });
    },
    getUserOptions: function () {
      this.$api.user.findUserOptions().then((res) => {
        this.userOptions = res.data;
      });
    },
    uploadImgImport(item) {
      this.itemFile = item.file;
    },
    // 导入事件
    beforeFirstAvatarUploadexcel(file) {
      let termId = this.importInline.termId;
      if (!termId) {
        this.$message.error("请选择导入的学期");
      }
      const isEXECL =
        file.type === `application/vnd.ms-excel` ||
        file.type ===
          `application/vnd.openxmlformats-officedocument.spreadsheetml.sheet`;
      if (!isEXECL) {
        this.$message.error("请导入正确的格式");
      }
      if (termId && isEXECL) {
        this.importInline.fileName = file.name;
      }
      return termId && isEXECL;
    },
    handleImport() {
      this.importDialogVisible = true;
    },
    importFile() {
      let formData = new FormData();
      formData.append("file", this.itemFile);
      formData.append("termId", this.importInline.termId);
      this.$api.busSynthesis.importSports(formData).then((res) => {
        this.editDialogVisible = false;
        this.findPage(null);
      });
    },
    // 获取部门列表
    findDeptTree: function () {
      this.$api.dept.findDeptTree().then((res) => {
        this.deptData = res.data;
      });
    },
    // 菜单树选中
    deptTreeCurrentChangeHandle(data, node) {
      this.filters.deptId = data.id;
      this.filters.deptName = data.name;
    },
    //导出
    handleExport() {
      if (this.filters.deptId == "" || this.filters.termId == "") {
        this.$message.error("请选择导出的学期和班级");
      } else {
        this.$api.busSynthesis.exportSports(this.filters).then((data) => {
          //let fileName = data.headers['content-disposition'].slice(data.headers['content-disposition'].indexOf('=') + 1)
          let fileName = new Date().valueOf() + "-体育.xlsx";
          //let type = data.headers[`content-type`].slice(0, data.headers[`content-type`].indexOf(';'))
          let blob = new Blob([data]);
          if ("download" in document.createElement("a")) {
            // 非IE下载
            const elink = document.createElement("a");
            elink.setAttribute("download", decodeURI(fileName));
            elink.style.display = "none";
            elink.href = URL.createObjectURL(blob);
            document.body.appendChild(elink);
            elink.click();
            URL.revokeObjectURL(elink.href); // 释放 URL对象
            document.body.removeChild(elink);
          } else {
            // IE10+下载
            navigator.msSaveBlob(blob, fileName);
          }
        });
      }
    },
    handleCalculation() {
      if (this.filters.deptId == "" || this.filters.termId == "") {
        this.$message.error("请选择计算的学期和班级");
      } else {
        this.$api.busSynthesis.calculationSports(this.filters).then((data) => {
          this.findPage(null);
        });
      }
    },
    handleClear() {
      if (this.filters.deptId == "" || this.filters.termId == "") {
        this.$message.error("请选择清空的学期和班级");
      } else {
        this.$api.busSynthesis.resetSports(this.filters).then((data) => {
          this.findPage(null);
        });
      }
    },
    async findCurrentTerm() {
      await this.$api.busTerm.findCurrentTerm().then((res) => {
        this.$set(this.filters, "termId", res.data.id);
      });
    },
  },
  mounted() {
    this.findCurrentTerm();
    this.getTermOptions();
    this.getUserOptions();
    this.findDeptTree();
  },
};
</script>

<style scoped>
</style>
